<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of View_purchase
 *
 * @author peerawatj20
 */
class view_purchase extends MY_Controller {

    public function __construct() {
        parent::__construct();
        $this->load->model('purchase_model');
        $this->load->model('catalog');
    }

    public function index() {
        $id = $this->input->get('id');
        $this->_set_form($id);
        $this->load->view('purchase/view_purchase', $this->data);
    }

    private function _set_form($id) {
        $utility = new Config_utility();
        $obj = $this->purchase_model->get_purchase_by_id($id);
        $this->data['lbl_created_date'] = '<h5>' .
                $utility->convert_date($obj[0]->purchase_created_date, $utility->DAY) . "  " .
                $utility->month_to_string($utility->convert_date($obj[0]->purchase_created_date, $utility->MONTH)) . "  " .
                $utility->convert_date($obj[0]->purchase_created_date, $utility->YEAR) .
                '</h5>';
        $this->_set_table($id, $obj);
    }

    private function _set_table($id) {
        $utility = new Config_utility();
        $obj = $this->purchase_model->get_order_by_id($id);
        $total_all = 0;
        $count = 0;

        for ($i = 0; $i < count($obj); $i++) {

            $row = '<tr id="tr_row' . $i . '"> 
                    <td> 
                        ' . ($i + 1) . ' 
                    </td> 
                    <td> 
                         <div id="lblLocatePurchase' . $i . '" style="height:22px;">' . $obj[$i]->locate_purchase . '</div> 
                    </td> 
                    <td> 
                         <div id="lblMainType' . $i . '" style="height:22px;">' . $obj[$i]->main_type_name . '</div> 
                    </td> 
                    <td> 
                         <div id="lblSubType' . $i . '" style="height:22px;">' . $obj[$i]->sub_type_name . '</div> 
                    </td> 
                    <td> 
                         <div id="lblBrand' . $i . '" style="height:22px;">' . $obj[$i]->brand_name . '</div> 
                    </td> 
                    <td> 
                        <div id="lblAutoPart' . $i . '" style="height:22px;">' . $obj[$i]->stock_name . '</div> 
                    </td> 
                    <td> 
                        <div id="lblQty' . $i . '" style="height:22px;" >' . $utility->format_number($obj[$i]->qty) . ' ' . $obj[$i]->classifire . '</div> 
                    </td> 
                    <td> 
                        <div id="lblPrice' . $i . '" style="height:22px;">' . $utility->format_number($obj[$i]->price) . ' บาท</div> 
                    </td> 
                    <td> 
                        <div id="lblTotalPrice' . $i . '" style="height:22px;">' . $utility->format_number($obj[$i]->price * $obj[$i]->qty) . ' บาท</div> 
                    </td> 
                </tr>';

            $total_all = $total_all + ($obj[$i]->price * $obj[$i]->qty);

            $this->data['row_table'][$i] = $row;
            $count++;
        }
        $total_price = '<tr><td colspan="8" class="text-right"><strong>ราคารวมทั้งหมด</strong></td><td><strong>' . $utility->format_number($total_all) . ' บาท</strong></td></tr>';
        $this->data['total_price'] = $total_price;
        $this->data['row_total'] = $count;
        $this->data['total_all'] = $total_all;
    }

}
